package com.cs.beneficios.web.action;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import co.gov.medellin.ssti.sub.common.excepcion.SUBException;

import com.cs.beneficios.business.entity.AuditoriaDAO;
import com.cs.beneficios.business.model.SubProgramaVO;
import com.cs.beneficios.common.ConnectionFactory;
import com.cs.paoyer.administration.business.model.UserVO;

public class CAGenerateReportAction extends Action {
	/**
	 * This is the main action called from the Struts framework.
	 * 
	 * @param mapping
	 *            The ActionMapping used to select this instance.
	 * @param form
	 *            The optional ActionForm bean for this request.
	 * @param request
	 *            The HTTP Request we are processing.
	 * @param response
	 *            The HTTP Response we are processing.
	 * @return
	 * @throws java.io.IOException
	 * @throws javax.servlet.ServletException
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		// ConnectionFactory cf = new
		// ConnectionFactory("beneficios","beneficios");
		ConnectionFactory cf = new ConnectionFactory();
		HttpSession session = request.getSession();
		UserVO user = (UserVO) request.getSession().getAttribute("userInfo");

		String strAdmin = (String) request.getParameter("administrador");
		String strCodeAdmin = (String) request.getParameter("admincodigo");
		String strCodeProg = (String) request.getParameter("codePrograma");
		String strCodeSubprog = (String) request.getParameter("code");
		String strDescription = null;
		Collection cReport = null;

		session.removeAttribute("cReport");

		// fecha inicial
		String strDesde = (request.getParameter("desde") == null) ? ""
				: request.getParameter("desde");
		String strHasta = (request.getParameter("hasta") == null) ? ""
				: request.getParameter("hasta");

		/*
		 * List lUsers = (List)session.getAttribute("cUsers"); UserVO objUserVO
		 * = new UserVO(); objUserVO = (UserVO)lUsers.get(0); String
		 * strDescription = objUserVO.getDescription();
		 */

		SubProgramaVO objSubpro = new SubProgramaVO();
		if (strAdmin != null)
			objSubpro.setNombreUsuario(strAdmin);
		if (strCodeProg != null && !strCodeProg.equals(""))
			objSubpro.setCodePrograma(new Long(strCodeProg));
		if (strCodeSubprog != null && !strCodeSubprog.equals(""))
			objSubpro.setCodeSubPrograma(strCodeSubprog);
		if (strDesde != null && !strDesde.equals(""))
			objSubpro.setDesdeVigencia(strDesde);
		if (strHasta != null && !strHasta.equals(""))
			objSubpro.setHastaVigencia(strHasta);
		try {
			AuditoriaDAO objAudiDAO = new AuditoriaDAO(cf);
			strDescription = objAudiDAO.getDescriptionUser(strAdmin);
			cReport = objAudiDAO.getReportControl(objSubpro);
			cf.closeConnection(objAudiDAO.getConn());

			if (cReport != null && cReport.size() != 0)
				session.setAttribute("cReport", cReport);

			if (strAdmin != null && !strAdmin.equals(""))
				request.setAttribute("usuario", strAdmin);
			if (strDescription != null && !strDescription.equals(""))
				request.setAttribute("descripcion", strDescription);
			if (strDesde != null && !strDesde.equals("")) {
				request.setAttribute("desde", strDesde);
			}
			if (strHasta != null && !strHasta.equals("")) {
				request.setAttribute("hasta", strHasta);
			}

		} catch (SQLException sqle) {
			SUBException.logError(sqle);
		}

		return mapping.findForward("success");
	}
}